package Player.IAStrategy;

import GameBoard.GameBoard;
import GameElements.Pawn;
import GameElements.Tile;
import Player.Player;

public class IAGloutonStrategy extends IAStrategy
{	
	public void execute(Player player , GameBoard board)
	{
		Tile[][] table = board.getTable();
		
		Tile tileToPlay = board.piocher();
		player.setTuilePioche(new Tile(tileToPlay));
		if(tileToPlay == null)
			return;
		
		for(int i=1 ; i< table.length-1; i++)
		{	
			for(int j=1 ; j< table[0].length-1; j++)
			{
				int nbRotation = 0;
				
				if(table[i-1][j]== null && table[i+1][j]== null && table[i][j-1]== null && table[i][j+1]== null){
				}
				else{
					while(nbRotation < 4)
					{
						if(!board.canAdd(tileToPlay, i, j))
						{
							tileToPlay.rotate90();
							nbRotation++;
						}
						else
						{
							board.add(tileToPlay, i, j);
							player.setTuilePioche(null);
							
							//try to add a pawn
							if(player.getPawn().size() > 0)
							{
								Pawn pawn = player.getPawn().get(0);
								if(pawn != null)
								{
									for(int indexPawnInTile=0; indexPawnInTile<13; indexPawnInTile++)
									{								
										if(board.canAdd(pawn, i, j, indexPawnInTile)){
											board.add(pawn, i, j, indexPawnInTile);
											player.getPawn().remove(pawn);
											break;
										}
									}
								}
							}
							return;
						}
					}
				}
			}
		}		
	}
}
